<?php

/***@property string $class_path
*@property integer $pid
*@property integer $task_id
*@property string $run_time
*@property string $intervals
*@property string $status
*@property string $description
 */

namespace backend\modules\tool\models;

use backend\modules\tool\DataSource\DataAdapter;
use backend\modules\tool\Job\SqlNode;

class DataSourceTask extends \backend\modules\tool\models\BaseModel
{
    /**
     * @inheritdoc
     */
    const tick=DataAdapter::tick;
    public static $create_sql="create table if not exists t_datasourcetask_back_fill(`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
      `class_path` varchar(50) DEFAULT NULL COMMENT '任务类的命名空间',
      `pid` int(50) DEFAULT NULL COMMENT '当前任务的pid',
      `task_id` int(50) DEFAULT NULL COMMENT '任务ID',
      `run_time` varchar(50) DEFAULT NULL COMMENT '运行时间',
      `intervals` varchar(50) DEFAULT NULL COMMENT '程序运行间隔时间minute-hour-day-week-month-year',
      `status` varchar(50) DEFAULT NULL COMMENT '是否启用',
      `description` varchar(50) DEFAULT NULL COMMENT '任务描述',
      `run_num` varchar(50) DEFAULT NULL COMMENT '执行次数',
       PRIMARY KEY (`id`)
    ) 
    ENGINE=InnoDB DEFAULT CHARSET=utf8;";
    public static function tableName()
    {
        return '{{%t_datasourcetask_back_fill}}';
    }
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['run_num'],'safe'],
            [['class_path','task_id','intervals'],'required'],
            [['class_path','run_time','intervals','description'],'string','max'=>'50'],
            [['pid','task_id','status'],'integer']
        ];
    }
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'class_path'=>'任务类的命名空间',
            'pid'=>'当前任务的pid',
            'task_id'=>'任务ID',
            'run_time'=>'运行时间',
            'intervals'=>'程序运行间隔时间minute-hour-day-week-month-year',
            'status'=>'是否启用',
            'description'=>'任务描述',
        ];
    }
    public static function NameSpace(){
        return [
            "SQL节点"=>SqlNode::class
        ];
    }
    public static function join($id,$time,$run_time){
        $model=new self();
        $model->task_id=$id;
        $model->intervals=$time;
        $model->class_path=SqlNode::class;
        $model->run_time=strtotime($run_time);
        $model->save();
    }
    public function nextTick(){
        $interval_info=explode("*",$this->intervals);
        $tick_falg=self::tick[$interval_info[1]];//获取当前的时间维度标识 week
        if(is_numeric($tick_falg)){
            $this->run_time=date("Y-m-d H:i:s",strtotime($this->run_time)+$tick_falg*$interval_info[0]);
            while (strtotime($this->run_time)<strtotime(date("Y-m-d H:i:s"))){
                $this->run_time=date("Y-m-d H:i:s",strtotime($this->run_time)+$tick_falg*$interval_info[0]);
            }
        }else{
            $this->run_time=date("Y-m-d H:i:s",strtotime($this->run_time))." +{$interval_info[0]} {$tick_falg}";
            while (strtotime($this->run_time)<strtotime(date("Y-m-d H:i:s"))){
                $this->run_time=date("Y-m-d H:i:s",strtotime($this->run_time))." +{$interval_info[0]} {$tick_falg}";
            }
        }
        if(is_numeric($this->run_num)){
            $this->run_num-=1;
        }
        $this->save();
    }
    public function beforeSave($insert)
    {
        if(empty($this->run_num)){
            $this->run_num="forever";
        }
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }
}